package service;

import dao.FileDao;
import dao.FileMeta;
import manager.FileManager;

import java.io.File;
import java.util.List;

/**
 * @author: cheng
 * @date: 2023/7/7 20:22
 * @description: 程序核心业务
 */
public class SearchService {
    private FileDao fileDao = new FileDao();
    private FileManager fileManager = new FileManager();
    //扫描线程
     private Thread t = null;

    //初始化操作
    public void init(String basePath){
        fileDao.initDB();

        t = new Thread(()->{
            while (!t.isInterrupted()){
                //扫描操作在线程中完成
                fileManager.scanAll(new File(basePath));
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        t.start();
        System.out.println("[SearchService] 初始化完成 ");
    }

    //停止扫描线程
    public void shutDown(){
        if (t != null){
            t.interrupt();
        }

    }
    //查找方法
     public List<FileMeta> search(String pattern){
        return fileDao.searchByPattern(pattern);
     }
}
